#pragma once 
#include"sock.hpp"
#include<pthread.h>
#include<signal.h>
#define PORT 8081

class tcp_server{
  private:
    int port;
    static tcp_server* tc;
    static pthread_mutex_t lock;
  public:
    int lisent_sock;
    static tcp_server* GetTcpObj(int port)
    {
      if(tc==nullptr){
        pthread_mutex_lock(&lock);
        if(tc==nullptr){
          tc=new tcp_server(port);
          signal(SIGPIPE,SIG_IGN);
          tc->Initserver();
        }
        pthread_mutex_unlock(&lock);
      }
      return tc;
    }
  public:
    tcp_server(int _port=PORT):port(_port),lisent_sock(-1)
    {}
    ~tcp_server(){
      if(lisent_sock>=0)close(lisent_sock);
    }
    void Initserver()
    {
      lisent_sock=sock_server::Sock();
      sock_server::Bind(lisent_sock,port);
      sock_server::Linst(lisent_sock);
      LOG(INFO,"Initserver success");
    }
    int sock()
    {
      return lisent_sock;
    }
};
tcp_server* tcp_server::tc=nullptr;
pthread_mutex_t tcp_server::lock=PTHREAD_MUTEX_INITIALIZER;

